package org.vdab.projects.tasks.server.dal;

import java.util.LinkedList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.vdab.projects.tasks.shared.EntityManager;
import org.vdab.projects.tasks.shared.entities.ITask;
import org.vdab.projects.tasks.shared.entities.ITaskBook;
import org.vdab.projects.tasks.shared.entities.ITaskList;
import org.vdab.projects.tasks.shared.entities.impl.Task;
import org.vdab.projects.tasks.shared.entities.impl.TaskBook;
import org.vdab.projects.tasks.shared.entities.impl.TaskList;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class ServerEntityManager extends EntityManager {


	public ServerEntityManager() {
	}

	
	public List<ITaskBook> getTaskBooks() throws Exception {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<ITaskBook> result = null;

		try {
			// DOC:
			// http://code.google.com/intl/nl-NL/appengine/docs/java/datastore/jdo/queries.html
			Query query = pm.newQuery(TaskBook.class);
			query.setFilter("ownerID == ownerParam");
			query.declareParameters("String ownerParam");

			@SuppressWarnings("unchecked")
			List<TaskBook> attachedList = (List<TaskBook>) query
					.execute(getCurrentUserId());

			for (TaskBook tb : attachedList) {
				tb.Load();
			}

			result = new LinkedList<ITaskBook>(pm.detachCopyAll(attachedList));

		} finally {
			pm.close();
		}
		return result;
	}

	public void saveTaskBook(ITaskBook taskBook) throws Exception {

		TaskBook entity = (TaskBook) taskBook;

		PersistenceManager pm = PMF.get().getPersistenceManager();

		entity.setOwnerId(getCurrentUserId());
		pm.makePersistent(entity);

	
		if(entity.hasLoadedTasklist()){
			pm.makePersistentAll(entity.getTaskLists());
		}
		pm.close();

	}

	private String getCurrentUserId() {
		UserService us = UserServiceFactory.getUserService();
		return us.getCurrentUser().getUserId();
	}

	public void saveTaskList(ITaskList taskList) {
		TaskList entity = (TaskList) taskList;
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			pm.makePersistent(entity);
		}finally{
			pm.close();
		}
	}

	
	public void saveTask(ITask task) {
		Task entity = (Task)task;
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			pm.makePersistent(entity);
		}finally{
			pm.close();
		}
	}
	
	public TaskList getTaskListByKey(String key) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		TaskList result;
		try {
			// DOC:
			// http://code.google.com/intl/nl-NL/appengine/docs/java/datastore/jdo/queries.html
			Query query = pm.newQuery(TaskList.class);
			query.setFilter("simpleKey == simpleKeyParam");
			query.declareParameters("String simpleKeyParam");
					
			TaskList attached = pm.getObjectById(TaskList.class, key);
			//Call it once while attached, so it will be loaded.
			attached.getTasks();
						
			result = pm.detachCopy(attached);
		} finally {
			pm.close();
		}
		return result;
	}
	
	public ITaskBook CreateTaskBook() {				
		return new TaskBook();
	}

	public ITaskList createTaskList(ITaskBook parrent) throws Exception {		
		TaskList tl = new TaskList("TaskList");
		tl.addToTodobook(parrent);
		return tl;
	}
}
